package com.rec.smile.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

import com.rec.smile.bo.SecKill;
import com.rec.smile.bo.SuccKillUser;
import com.rec.smile.dto.Exposer;
import com.rec.smile.exception.BusinessException;
import com.rec.smile.exception.ErrorCode;
import com.rec.smile.mapper.SecKillMapper;
import com.rec.smile.mapper.SuccKillUserMapper;
import com.rec.smile.service.SecKillService;
import com.rec.smile.util.TimeUtil;

@Service("secKillService")
public class SecKillServiceImpl implements SecKillService{
	
	private static final String md5_salt = "daauaskqwqmmx,czlf;'zzxc";

	@Autowired
	private SecKillMapper secKillMapper;
	
	@Autowired
	private SuccKillUserMapper succKillUserMapper;
	@Override
	public List<SecKill> getSecKills() {
		return secKillMapper.queryBy(0, 4);
	}

	@Override
	public SecKill getSecKill(long id) {
		return secKillMapper.selectByPrimaryKey(id);
	}

	@Override
	public Exposer export2SecKillUrl(Long id) {
		if(id == null){
			throw new BusinessException(ErrorCode.not_exist_sec_kill,"此类道具未参与秒杀！");
		}
		SecKill secKill = secKillMapper.selectByPrimaryKey(id);
		if(secKill == null){
			throw new BusinessException(ErrorCode.not_exist_sec_kill,"此类道具未参与秒杀！");
		}
		long now = TimeUtil.currentTimeMillis();
		if(secKill.getStartTime().getTime() > now){
			throw new BusinessException(ErrorCode.sec_kill_not_start,"此类道具未参与秒杀！");
		}
		if(secKill.getNum() == 0 || secKill.getEndTime().getTime() < now){
			throw new BusinessException(ErrorCode.sec_kill_close,"此类道具未参与秒杀！");
		}
		
		return new Exposer(true, getMd5(secKill.getId()), secKill.getId(), now, secKill.getStartTime().getTime(), secKill.getEndTime().getTime());
	}

	@Transactional
	@Override
	public SuccKillUser execSecKill(Long id, String phone,String md5) {
		if(id == null){
			throw new BusinessException(ErrorCode.not_exist_sec_kill,"此类道具未参与秒杀！");
		}
		if(md5 == null || !getMd5(id).equals(md5)){
			throw new BusinessException(ErrorCode.data_rewite, "数据篡改");
		}
		if(phone == null || "".equals(phone.trim())){
			throw new BusinessException(ErrorCode.sec_kill_not_login, "没有登陆");
		}
		try{
			Date now = new Date(TimeUtil.currentTimeMillis());
			int updateCount = secKillMapper.reduceSecKillNum(id, now);
			if(updateCount <= 0){
				throw new BusinessException(ErrorCode.sec_kill_close,"此类道具未参与秒杀！");
			}
			
			SuccKillUser record = new SuccKillUser();
			record.setCreateTime(now);
			record.setSecKillId(id);
			record.setStatus((byte) 0);
			record.setPhoneNum(phone);
			int count = succKillUserMapper.insertSelective(record);
			if(count <= 0){
				throw new BusinessException(ErrorCode.sec_kill_failed,"此类道具未参与秒杀！");
			}
			return record;
		}catch(Exception e){
			if(! (e instanceof RuntimeException)){
				throw new RuntimeException(e);
			}
			throw e;
		}
	}
	
	
	private static String getMd5(long secKillId){
		return DigestUtils.md5DigestAsHex((secKillId +"/"+md5_salt).getBytes());
	}

}
